package mysql

import (
	"gorm.io/gorm"
	"video-guard/videorpc/videorpc"
)

type CollectionInfo struct {
	gorm.Model
	UserId        int `gorm:"type:int(11)"`
	VideoId       int `gorm:"type:int(11)"`
	CollectStatus int `gorm:"type:int(11)"`
}

func (i *CollectionInfo) FindCollectionById(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("user_id=? and video_id=?", id, id2).Find(i).Error
}

func (i *CollectionInfo) CollectionAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CollectionInfo) FirstCollectionById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(i).Error
}

func (i *CollectionInfo) CollectionUpdate(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Updates(i).Error
}

func CollectionList(db *gorm.DB, page int64, size int64) ([]*videorpc.CollectionList, error) {

	var collectionList []*videorpc.CollectionList

	err2 := db.Debug().Model(&CollectionInfo{}).Select(
		"video_infos.cover_link",
		"video_infos.video_title",
		"user_infos.nick_name",
		"video_infos.play_num",
	).Joins("LEFT JOIN video_infos ON video_infos.id=collection_infos.video_id").
		Joins("LEFT JOIN user_infos ON user_infos.id=collection_infos.user_id").
		Scopes(Paginate(int(page), int(size))).
		Order("collection_infos.created_at DESC").
		Find(&collectionList).Error

	return collectionList, err2
}
